import { ApiResp } from '@main/bo/ApiResp'
import BizException from '@main/exception/BizException'
import { ArgumentsHost, Catch, ExceptionFilter } from '@nestjs/common'
import { Request, Response } from 'express'

@Catch(BizException)
export class BizExceptionFilter implements ExceptionFilter<BizException> {
  catch(exception: BizException, host: ArgumentsHost) {
    const ctx = host.switchToHttp() // 获取请求上下文
    const request = ctx.getRequest<Request>() // 获取请求上下文中的request对象
    const response = ctx.getResponse<Response>() // 获取请求上下文中的response对象
    const path = request.url
    // 设置返回的状态码， 请求头，发送错误信息
    response.status(200).json(ApiResp.error(400, exception.message, path))
  }
}
